package com.taotao.cat.controller;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.CookieUtils;
import com.taotao.common.utils.JsonUtils;
import com.taotao.pojo.TbItem;
import com.taotao.service.ItemService;

@Controller
public class CartController {
	@Autowired
	private ItemService itemService;
	@Value("${CART_COOKIR_KEY}")
	private String CART_COOKIR_KEY;
	@Value("${CART_COOKIE_EXPIRE}")
	private Integer CART_COOKIE_EXPIRE;
	@RequestMapping("/cart/add/{itemId}")
	public String addCartItem(@PathVariable Long itemId,Integer num,HttpServletRequest request,HttpServletResponse response){
		//从cookie中取出购物车商品列表
		List<TbItem> cartList = getCartList(request);
		boolean flag = false;
		for (TbItem tbItem : cartList) {
			if(tbItem.getId() == itemId.longValue()){
				tbItem.setNum(tbItem.getNum() + num);
				flag = true;
				break;
			}
		}
		if (! flag) {
			//从数据库中查询商品
			TbItem tbItem = itemService.getItemById(itemId);
			tbItem.setNum(num);
			String image = tbItem.getImage();
			if(StringUtils.isNoneBlank(image)){
				tbItem.setImage(image.split(",")[0]);
			}
			cartList.add(tbItem);
			CookieUtils.setCookie(request, response, CART_COOKIR_KEY, JsonUtils.objectToJson(cartList), CART_COOKIE_EXPIRE, true);
		}
		return "cartSuccess";
	}
	public List<TbItem> getCartList(HttpServletRequest request){
		String value = CookieUtils.getCookieValue(request, CART_COOKIR_KEY, true);
		if (StringUtils.isBlank(value)) {
			return new ArrayList<>();
		}
		List<TbItem> list = JsonUtils.jsonToList(value, TbItem.class);
		return list;
	}
	
	@RequestMapping("/cart/cart")
	public String showCartList(HttpServletRequest request,Model model){
		List<TbItem> cartList = getCartList(request);
		model.addAttribute("cartList", cartList);
		return "cart";
	}
	@RequestMapping("/cart/update/num/{itemId}/{num}")
	@ResponseBody
	public TaotaoResult updateNum(@PathVariable Long itemId,@PathVariable Integer num,HttpServletRequest request,HttpServletResponse response){
		List<TbItem> cartList = getCartList(request);
		for (TbItem tbItem : cartList) {
			if(tbItem.getId() == itemId.longValue()){
				tbItem.setNum(num);
				break;
			}
		}
		//重新写入cookie
		CookieUtils.setCookie(request, response, CART_COOKIR_KEY, JsonUtils.objectToJson(cartList), CART_COOKIE_EXPIRE, true);
		return TaotaoResult.ok();
	}
	@RequestMapping("/cart/delete/{itemId}")
	public String deleteCart(@PathVariable Long itemId,HttpServletRequest request,HttpServletResponse response){
		//查询cookie中的购物车信息
		List<TbItem> cartList = getCartList(request);
		for (TbItem tbItem : cartList) {
			if (tbItem.getId().longValue() == itemId) {
				cartList.remove(tbItem);
				break;
			}
		}
		//重新写入cookie
		CookieUtils.setCookie(request, response, CART_COOKIR_KEY, JsonUtils.objectToJson(cartList), CART_COOKIE_EXPIRE, true);
		return "redirect:/cart/cart.html";
	}
}
